home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / networking / amitcp / amislate1.0.lha / AmiSlate1.0 / SlateRexx / tictactoe.rexx < prev   
Encoding:
OS/2 REXX Batch file  |  1995-03-13  |  13.5 KB  |  455 lines

  1. /* TicTacToe for AmiSlate v1.0! */
  2.  
  3. /* Constants for use with AmiSlate's ARexx interface */
  4. AMode.DOT      =  0 
  5. AMode.PEN      =  1 
  6. AMode.LINE     =  2 
  7. AMode.CIRCLE   =  3 
  8. AMode.SQUARE   =  4 
  9. AMode.POLY     =  5 
  10. AMode.FLOOD    =  6 
  11. AMode.CLEAR    =  7 
  12.  
  13. AMessage.TIMEOUT     = 1    /* No events occurred in specified time period */
  14. AMessage.MESSAGE     = 2    /* Message recieved from remote Amiga */
  15. AMessage.MOUSEDOWN   = 4    /* Left mouse button press in drawing area */
  16. AMessage.MOUSEUP     = 8    /* Left mouse button release in drawing area */
  17. AMessage.RESIZE      = 16    /* Window was resized--time to redraw screen? */ 
  18. AMessage.QUIT        = 32    /* AmiSlate is shutting down */
  19. AMessage.CONNECT     = 64    /* Connection established */
  20. AMessage.DISCONNECT  = 128    /* Connection broken */
  21. AMessage.TOOLSELECT  = 256    /* Tool Selected */
  22. AMessage.COLORSELECT = 512    /* Palette Color selected */
  23. AMessage.KEYPRESS    = 1024    /* Key pressed */
  24.  
  25. /* Get our host's name--always given as first argument when run from Amislate */
  26. parse arg CommandPort ActiveString
  27.  
  28. /* Send all commands to this host */
  29. address (CommandPort) 
  30.  
  31. options results
  32.  
  33. /* Reserves pixels for a future toolbar -- currently, none */
  34. ToolBarHeight = 0
  35.  
  36. /* Check to see which tool is selected, whether we are connected */
  37. BFlood = 0
  38.  
  39. /* Parse command line argument to see if we've been activated by 
  40.    a remote request or a local user */
  41. check = upper(left(ActiveString,3))
  42. if (upper(left(ActiveString,3)) ~= 'RE') then 
  43.     do
  44.         BActive = 1
  45.     end
  46.     else
  47.     do    
  48.         BActive = 2
  49.     end
  50.  
  51. /* See if we're connected */
  52. GetRemoteStateAttrs stem rstateattrs.
  53.  
  54. if (rstateattrs.mode > -1) then 
  55.     do
  56.         BConnectMode = 1
  57.     end
  58.     else
  59.     do
  60.         BConnectMode = 0
  61.     end
  62.     
  63. /* Disable drawing */
  64. lock on
  65.  
  66. /* Initialize TicTacToe board */
  67. success = InitTTTArray()
  68.  
  69. /* Initiator (X) goes first */
  70. turn = 1
  71. moves = 0
  72.  
  73. /* Handshaking for two-computer game */
  74. if (BConnectMode = 1) then 
  75. do
  76.     if (BActive == 1) then 
  77.     do
  78.  
  79.         SetWindowTitle '"'||"Requesting game from remote user"||'"' 
  80.     RemoteRexxCommand '"'||"Would you like to play TicTacToe?"||'"' "slaterexx:TicTacToe.rexx"
  81.     
  82.         waitevent stem handshake. MESSAGE
  83.         if (handshake.message == 0) then 
  84.         do
  85.             SetWindowTitle '"'||"TicTacToe Game Refused"||'"'
  86.             exit
  87.         end
  88.     success = DrawTTTBoard()
  89.     end
  90.     else
  91.     do
  92.         /* Examine window to get dimensions */
  93.     GetWindowAttrs stem winattrs.
  94.        BoardWidth = winattrs.width  - 58
  95.        BoardHeight= winattrs.height - 53 - ToolBarHeight
  96.     end
  97. end
  98. else 
  99. do
  100.     success = DrawTTTBoard()
  101. end        
  102.  
  103. success = UpdateStatus()
  104. do while(1)    
  105.     waitevent stem event. RESIZE MOUSEUP MESSAGE TOOLSELECT DISCONNECT QUIT
  106.     if ((event.type == AMessage.TOOLSELECT)&(event.code1 = AMode.CLEAR)) then do
  107.         SetWindowTitle '"'||"Starting New Game"||'"'
  108.         success = InitTTTArray()
  109.         success = DrawTTTBoard()
  110.         
  111.         /* Tell partner that the board has been cleared */
  112.         if (BConnectMode == 1) then SendMessage 99
  113.         end
  114.         
  115.     if (event.type == AMessage.DISCONNECT) then BConnectMode = 0
  116.     if (event.type == AMessage.QUIT) then exit
  117.     if (event.type == AMessage.RESIZE) then do
  118.         if ((BActive == 1)|(BConnectMode == 0)) then do
  119.            success = DrawTTTBoard()
  120.         end
  121.         else do
  122.            /* Just examine window to get new dimensions */
  123.            GetWindowAttrs stem winattrs.
  124.            BoardWidth = winattrs.width  - 58
  125.            BoardHeight= winattrs.height - 53 - ToolBarHeight
  126.         end
  127.              success = UpdateStatus()
  128.     end
  129.         
  130.     if (event.type == AMessage.MESSAGE) then do
  131.         if (event.message == 99) then do
  132.                 success = InitTTTArray()
  133.                 success = UpdateStatus()
  134.             end
  135.             else do
  136.                 if (turn ~= BActive) then success = ParseMove(event.message)
  137.             end
  138.             end
  139.             
  140.     if ((moves < 9)&((event.type == AMessage.MOUSEUP)&((turn == BActive)|(BConnectMode == 0)))) then 
  141.     do
  142.         xx = 3     /* default */
  143.         if (event.x < (2*(BoardWidth / 3))) then xx = 2
  144.         if (event.x < (BoardWidth / 3)) then xx = 1
  145.         
  146.         yy = 3     /* default */
  147.         if (event.y < (2*(BoardHeight / 3))) then yy = 2
  148.         if (event.y < (BoardHeight / 3)) then yy = 1
  149.         
  150.         if (TTTBoard.xx.yy > 0) then do
  151.             SetWindowTitle '"'||"You can't move there!"||'"'
  152.             end
  153.         else do
  154.             success = DoMove(xx,yy)
  155.             end
  156.     end
  157. end
  158.  
  159. exit
  160.  
  161. /* --------------------------------------------------------------- */
  162. /* procedure DoMove                           */
  163. /* --------------------------------------------------------------- */
  164. DoMove: procedure expose TTTBoard. turn moves BConnectMode BActive BoardWidth BoardHeight ToolBarHeight
  165.     parse arg xx,yy 
  166.     
  167.     TTTBoard.xx.yy = turn 
  168.  
  169.     if ((BConnectMode == 0)|(turn == BActive)) then success = DrawMove(xx,yy)
  170.     if ((BConnectMode == 1)&(turn == BActive)) then do
  171.         message = xx||yy
  172.         SendMessage message
  173.         end    
  174.                 
  175.     if (turn == 1) then do
  176.         turn = 2
  177.         end
  178.         else do
  179.         turn = 1
  180.         end
  181.     moves=moves+1
  182.     success = CheckForWins()
  183.     if ((success > 0)|(moves>=9)) then do
  184.         moves = 9 /* disallow more movement */
  185.         if (success == 1) then SetWindowTitle '"'||"X's won!  Click CLR to play again" ||'"'
  186.         if (success == 2) then SetWindowTitle '"'||"O's won!  Click CLR to play again" ||'"'
  187.         if (success == 0) then SetWindowTitle '"'||"Cat's game!  Click CLR to play again" ||'"'
  188.         end
  189.         else do
  190.         success = UpdateStatus()
  191.         end
  192.     return 1
  193.     
  194.  
  195. /* --------------------------------------------------------------- */
  196. /* procedure CheckForWins                       */
  197. /* --------------------------------------------------------------- */
  198. CheckForWins: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn BActive
  199.  
  200.     i=1
  201.     do while (i<4)
  202.         if ((TTTBoard.i.1==1)&(TTTBoard.i.2==1)&(TTTBoard.i.3==1)) then do
  203.             if (turn == BActive) then do
  204.                 SetFColor 15 15 15
  205.                 BHeight = BoardHeight - ToolBarHeight
  206.                 if (i==1) then barleft = trunc(BoardWidth*.13)
  207.                 if (i==2) then barleft = trunc(BoardWidth*.47)
  208.                 if (i==3) then barleft = trunc(BoardWidth*.82)
  209.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95)) fill
  210.                 SetFColor 0 0 0
  211.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95))
  212.                 end
  213.             return 1
  214.             end
  215.             
  216.         if ((TTTBoard.i.1==2)&(TTTBoard.i.2==2)&(TTTBoard.i.3==2)) then do
  217.             if (turn == BActive) then do
  218.                 SetFColor 15 15 15
  219.                 BHeight = BoardHeight - ToolBarHeight
  220.                 if (i==1) then barleft = trunc(BoardWidth*.13)
  221.                 if (i==2) then barleft = trunc(BoardWidth*.47)
  222.                 if (i==3) then barleft = trunc(BoardWidth*.82)
  223.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95)) fill
  224.                 SetFColor 0 0 0
  225.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95))
  226.                 end
  227.             return 2
  228.             end
  229.             
  230.         i = i + 1
  231.         end
  232.     i=1
  233.     do while (i<4)
  234.         if ((TTTBoard.1.i==1)&(TTTBoard.2.i==1)&(TTTBoard.3.i==1)) then do
  235.             if (turn == BActive) then do
  236.                 SetFColor 15 15 15
  237.                 BHeight = BoardHeight - ToolBarHeight
  238.                 if (i==1) then bartop = trunc((BHeight*.13)+ToolBarHeight)
  239.                 if (i==2) then bartop = trunc((BHeight*.47)+ToolBarHeight)
  240.                 if (i==3) then bartop = trunc((BHeight*.82)+ToolBarHeight)
  241.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20)) fill
  242.                 SetFColor 0 0 0
  243.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20))
  244.                 end
  245.             return 1
  246.             end
  247.  
  248.         if ((TTTBoard.1.i==2)&(TTTBoard.2.i==2)&(TTTBoard.3.i==2)) then do
  249.             if (turn == BActive) then do
  250.                 SetFColor 15 15 15
  251.                 BHeight = BoardHeight - ToolBarHeight
  252.                 if (i==1) then bartop = trunc((BHeight*.13)+ToolBarHeight)
  253.                 if (i==2) then bartop = trunc((BHeight*.47)+ToolBarHeight)
  254.                 if (i==3) then bartop = trunc((BHeight*.82)+ToolBarHeight)
  255.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20)) fill
  256.                 SetFColor 0 0 0
  257.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20))
  258.                 end
  259.             return 2
  260.             end
  261.             
  262.         i = i + 1
  263.         end
  264.  
  265.     if ((TTTBoard.1.1==1)&(TTTBoard.2.2==1)&(TTTBoard.3.3==1)) then return 1
  266.     if ((TTTBoard.1.1==2)&(TTTBoard.2.2==2)&(TTTBoard.3.3==2)) then return 2
  267.     if ((TTTBoard.3.1==1)&(TTTBoard.2.2==1)&(TTTBoard.1.3==1)) then return 1
  268.     if ((TTTBoard.3.1==2)&(TTTBoard.2.2==2)&(TTTBoard.1.3==2)) then return 2
  269.     
  270.     return 0
  271.     
  272. /* --------------------------------------------------------------- */
  273. /* procedure DrawMove                           */
  274. /* --------------------------------------------------------------- */
  275. DrawMove: procedure expose BActive turn BoardWidth BoardHeight ToolBarHeight TTTBoard.
  276.     parse arg xx,yy
  277.  
  278.     if (TTTBoard.xx.yy == 0) then return 1
  279.     
  280.     BHeight = BoardHeight - ToolBarHeight
  281.     
  282.     if (yy == 1) then do
  283.         ytop = ToolBarHeight
  284.         ybot = trunc(BHeight*.3)+ToolBarHeight
  285.         end
  286.         
  287.     if (yy == 2) then do
  288.         ytop = trunc(BHeight*.36)+ToolBarHeight
  289.         ybot = trunc(BHeight*.63)+ToolBarHeight
  290.         end
  291.         
  292.     if (yy == 3) then do
  293.         ytop = trunc(BHeight*.69)+ToolBarHeight 
  294.         ybot = trunc(BHeight*.99)+ToolBarHeight
  295.         end
  296.         
  297.     if (xx == 1) then do
  298.         xleft = 1
  299.         xright = trunc(BoardWidth*.3)
  300.         end
  301.         
  302.     if (xx == 2) then do
  303.         xleft = trunc(BoardWidth*.36)
  304.         xright = trunc(BoardWidth*.63)
  305.         end
  306.         
  307.     if (xx == 3) then do
  308.         xleft = trunc(BoardWidth*.69)
  309.         xright = trunc(BoardWidth*.99)
  310.         end
  311.         
  312. /*    square xleft ytop xright ybot fill */
  313.     if (TTTBoard.xx.yy == 1) then do
  314.         penreset
  315.         height = ybot - ytop
  316.         width  = xright - xleft
  317.         th = 3
  318.  
  319.         SetFColor 0 0 0 notbackground
  320.         pen trunc(xleft+(width/th))     ytop
  321.         pen trunc(xleft+(width/2))     trunc(ytop+(height/th))
  322.         pen trunc(xright-(width/th))     ytop
  323.         pen xright             trunc(ytop+(height/th))
  324.         pen trunc(xright-(width/th))     trunc(ytop+(height/2))
  325.         pen xright             trunc(ybot-(height/th))
  326.         pen trunc(xright-(width/th))     ybot
  327.         pen trunc(xright-(width/2))     trunc(ybot-(height/th))
  328.         pen trunc(xleft+(width/th))     ybot
  329.         pen xleft             trunc(ybot-(height/th))
  330.         pen trunc(xleft+(width/th))     trunc(ybot-(height/2))
  331.         pen xleft             trunc(ytop+(height/th))
  332.         pen trunc(xleft+(width/th))    ytop
  333.  
  334.         SetFColor 15 0 0 notbackground
  335.         flood trunc((xleft + xright)/2) trunc((ytop + ybot)/2)
  336.         end
  337.     else do
  338.         SetFColor 0 0 0 notbackground
  339.         circle trunc((xleft+xright)/2) trunc((ytop+ybot)/2) trunc((xright - xleft)/2) trunc((ybot - ytop)/2)
  340.         circle trunc((xleft+xright)/2) trunc((ytop+ybot)/2) trunc((xright - xleft)/3) trunc((ybot - ytop)/3)
  341.  
  342.         SetFColor 0 15 0 notbackground
  343.         flood trunc(((xleft+xright)/2)+((xleft-xright)/2.5)) trunc((ytop+ybot)/2)
  344.         end
  345.     return 1
  346.     
  347.         
  348.  
  349.  
  350. /* --------------------------------------------------------------- */
  351. /* procedure UpdateStatus                       */
  352. /* --------------------------------------------------------------- */
  353. UpdateStatus: procedure expose BActive turn BConnectMode moves
  354.  
  355.     if (moves > 8) then do
  356.         SetWindowTitle '"'||"Game Over, click CLR to play again"||'"'
  357.         return 1
  358.         end
  359.         
  360.     if ((BActive == turn)|(BConnectMode == 0)) then do
  361.         if (turn == 1) then SetWindowTitle '"'||"It's Your Turn, Player X"||'"'
  362.         if (turn == 2) then SetWindowTitle '"'||"It's Your Turn, Player O"||'"'
  363.     end
  364.     else
  365.     do
  366.         if (turn == 1) then SetWindowTitle '"'||"It's Their Turn (Player X)"||'"'
  367.         if (turn == 2) then SetWindowTitle '"'||"It's Their Turn (Player O)"||'"'
  368.     end
  369.     
  370.     return 1
  371.  
  372. /* --------------------------------------------------------------- */
  373. /* procedure InitTTTArray                       */
  374. /* --------------------------------------------------------------- */
  375. InitTTTArray: procedure expose TTTBoard. moves turn
  376.     TTTBoard.1.1 = 0
  377.     TTTBoard.1.2 = 0
  378.     TTTBoard.1.3 = 0
  379.     TTTBoard.2.1 = 0
  380.     TTTBoard.2.2 = 0
  381.     TTTBoard.2.3 = 0
  382.     TTTBoard.3.1 = 0
  383.     TTTBoard.3.2 = 0
  384.     TTTBoard.3.3 = 0
  385.     turn  = 1
  386.     moves = 0
  387.     return 1
  388.     
  389.  
  390. /* --------------------------------------------------------------- */
  391. /* procedure DrawTTTBoard                       */
  392. /* --------------------------------------------------------------- */
  393. DrawTTTBoard: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn BActive moves
  394.  
  395.    /* Say what we're doing */
  396.    SetWindowTitle '"'||"Drawing TicTacToe board, Please Wait"||'"'
  397.    SetRemoteWindowTitle '"'||"Drawing TicTacToe board, Please Wait"||'"'
  398.    
  399.    /* Examine window to get dimensions */
  400.    GetWindowAttrs stem winattrs.
  401.    BoardWidth = winattrs.width  - 58
  402.    BoardHeight= winattrs.height - 53
  403.  
  404.    say "Draw Height =  " winattrs.height
  405.  
  406.    /* Clear Screen */
  407.    clear
  408.  
  409.    /* Draw Board */
  410.    SetFColor 0 0 0 notbackground
  411.  
  412.    /* Height of Board */
  413.    BHeight = BoardHeight - ToolBarHeight   
  414.    
  415.    square (trunc(BoardWidth*.31)) ToolBarHeight (trunc(BoardWidth*.35)) BoardHeight fill
  416.    square (trunc(BoardWidth*.64)) ToolBarHeight (trunc(BoardWidth*.68)) BoardHeight fill
  417.    square 0 (trunc(BHeight*.31)+ToolBarHeight) BoardWidth (trunc(BHeight*.35)+ToolBarHeight) fill
  418.    square 0 (trunc(BHeight*.64)+ToolBarHeight) BoardWidth (trunc(BHeight*.68)+ToolBarHeight) fill
  419.    
  420.    success=DrawMove(1,1)
  421.    success=DrawMove(2,1)
  422.    success=DrawMove(3,1)
  423.    success=DrawMove(1,2)
  424.    success=DrawMove(2,2)
  425.    success=DrawMove(3,2)
  426.    success=DrawMove(1,3)
  427.    success=DrawMove(2,3)
  428.    success=DrawMove(3,3)
  429.  
  430.    success=CheckForWins()
  431.    if (success == 0) then success=UpdateStatus()
  432.    return 1
  433.    
  434.  
  435. /* --------------------------------------------------------------- */
  436. /* procedure ParseMove                           */
  437. /* --------------------------------------------------------------- */
  438. ParseMove: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn moves BActive BConnectMode
  439.     parse arg message
  440.  
  441.     xx=left(message,1)
  442.     yy=right(message,1)
  443.  
  444.     if ((xx>3)||(xx<0)|(yy>3)||(yy<0)) then do
  445.         SetWindowTitle '"'||"TicTacToe Transmission Trouble :("||'"'
  446.         return 0
  447.         end
  448.         
  449.     if (TTTBoard.xx.yy > 0) then do
  450.         EasyRequest '"'||"TicTacToe Message"||'"' '"'||"Your opponent is cheating (" || xx yy TTTBoard.xx.yy || ") !"||'"' '"'||"What a maroon"||'"'
  451.     end
  452.     else do
  453.         success=DoMove(xx,yy) 
  454.     end
  455.     return 1